package com.q.tqb.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.q.tqb.R;
import com.q.tqb.activity.ViewPagerImageActivity;

/**
 * A simple {@link Fragment} subclass.
 */
public class ImageFragment extends Fragment {


    private ImageView iv_image;
    private int image;
    private String[] images;

    public ImageFragment() {
        // Required empty public constructor
    }

    public static ImageFragment newInstance(int image, String[] images) {
        ImageFragment fragment = new ImageFragment();
        Bundle args = new Bundle();
        args.putInt("image", image);
        args.putStringArray("images", images);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            image = getArguments().getInt("image");
            images = getArguments().getStringArray("images");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_image, container, false);
        iv_image = view.findViewById(R.id.iv_image);


        Glide
                .with(getActivity())
                .load(images[image])
                .into(iv_image);

        iv_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), ViewPagerImageActivity.class);
                intent.putExtra("images", images);
                intent.putExtra("position", image);
                startActivity(intent);
            }
        });

        return view;
    }

}
